package cn.stylefeng.guns.modular.business.frequency.ordertake.service.impl;

import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.modular.business.custom.enums.OrderStatusEnum;
import cn.stylefeng.guns.modular.business.custom.enums.TakeCarTypeEnum;
import cn.stylefeng.guns.modular.business.frequency.ordercarverify.mapper.OrderCarVerifyMapper;
import cn.stylefeng.guns.modular.business.frequency.orderpick.model.param.TakeRecordContractVO;
import cn.stylefeng.guns.modular.business.frequency.ordersub.model.result.PkgTake;
import cn.stylefeng.guns.modular.business.frequency.ordertake.entity.OrderTake;
import cn.stylefeng.guns.modular.business.frequency.ordertake.mapper.OrderTakeMapper;
import cn.stylefeng.guns.modular.business.frequency.ordertake.model.param.TakeDTO;
import cn.stylefeng.guns.modular.business.frequency.ordertake.model.result.PkgOrderDelivery;
import cn.stylefeng.guns.modular.business.frequency.ordertake.service.OrderTakeService;
import cn.stylefeng.guns.modular.business.util.BusinessUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * tt_order_take 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:16
 */
@Service
public class OrderTakeServiceImpl extends ServiceImpl<OrderTakeMapper, OrderTake> implements OrderTakeService {

    @Autowired
    private BusinessUtil businessUtil;
    @Autowired
    private OrderTakeMapper orderTakeMapper;

    @Autowired
    private OrderCarVerifyMapper orderCarVerifyMapper;

    @Override
    public Page<PkgOrderDelivery> getWaitTakeCarOrderList(TakeDTO orderDTO) {
        orderDTO.setTenantId(businessUtil.getTenantId());

        Page<PkgOrderDelivery> list = baseMapper.getWaitTakeCarOrderList(PageFactory.defaultPage(), orderDTO);

        if (list != null && !list.getRecords().isEmpty()) {
            for (PkgOrderDelivery orderDelivery : list.getRecords()) {
                // 签收状态 1.待签收 2.已签收 3 ---
                if (orderDelivery.getStatus() == OrderStatusEnum.HAS_DEPART_ORDER.status) {
                    orderDelivery.setSignStatus(1); // 待签收
                } else {
                    orderDelivery.setSignStatus(2);
                }
                // 送车状态处理
                if (orderDelivery.getTakeType() == TakeCarTypeEnum.SHANG_MEN_TI_CHE.type || orderDelivery.getTakeType() == TakeCarTypeEnum.XIAO_BAN_SHANG_MEN.type) {
                    // 需要送车
                    if (orderDelivery.getStatus() == OrderStatusEnum.FINISHED_TRANSPORT.status) {
                        if (orderDelivery.getTakeType() == 1) {
                            orderDelivery.setTakeCarStatus(1);
                        } else {
                            orderDelivery.setTakeCarStatus(2);
                        }
                    } else {
                        // 有无送车
                        if (StringUtils.isNotBlank(orderDelivery.getSettlementName())) {
                            orderDelivery.setTakeCarStatus(6);
                        } else {
                            orderDelivery.setTakeCarStatus(3);
                        }
                    }
                    if (orderDelivery.getStatus() == OrderStatusEnum.WAIT_ASSIGN_TAKE_DRIVER.status) {
                        orderDelivery.setDriverStatus(1);
                    } else if (orderDelivery.getStatus() == OrderStatusEnum.WAIT_TAKE.status) {
                        // 有无送车
                        if (StringUtils.isNotBlank(orderDelivery.getSettlementName())) {
                            orderDelivery.setDriverStatus(2);
                        } else {
                            orderDelivery.setDriverStatus(3);
                        }
                    } else {
                        orderDelivery.setDriverStatus(3);
                    }
                } else {
                    orderDelivery.setTakeCarStatus(3);
                    orderDelivery.setDriverStatus(3);
                }
                // 交付状态
                if (orderDelivery.getStatus() == OrderStatusEnum.WAIT_TAKE.status) {
                    orderDelivery.setFinishStatus(1);
                } else {
                    orderDelivery.setFinishStatus(2);
                }


                //获取验车图片
                if (StringUtils.isNotBlank(orderDelivery.getValidateImagesStr())) {
                    String[] split = orderDelivery.getValidateImagesStr().split(",");
                    orderDelivery.setValidateImages(Arrays.asList(split));
                }
            }


        }

//        List<Long> orderIdList = list.getRecords().stream().map(x -> x.getOrderId()).collect(Collectors.toList());
//        Map<Long, PkgTake> pkgTakeMap = getPkgTakeMap(orderIdList);
//        for (PkgOrderDelivery delivery : list.getRecords()) {
//            if(pkgTakeMap.get(delivery.getOrderId())!=null) {
//                delivery.setTakeCarFee(pkgTakeMap.get(delivery.getOrderId()).getTakeCarFee());
//            }
//
//        }

        return list;
    }


    @Override
    public Map<Long, PkgTake> getPkgTakeMap(List<Long> orderIdList) {
        Map<Long, PkgTake> map = new HashMap<>();
        if (orderIdList.size() > 0) {
            List<PkgTake> pkgTakeList = orderTakeMapper.getPkgTakeList(orderIdList);
            pkgTakeList.stream().forEach(x -> {
                map.put(x.getId(), x);
            });
        }
        return map;
    }

    @Override
    public List<PkgOrderDelivery> getWaitTakeCarOrderList(TakeRecordContractVO orderDTO) {
        return baseMapper.getWaitTakeCarOrderList1(orderDTO);
    }


}